package edu.fivegroup.mall.consumer;

import com.google.gson.Gson;
import edu.fivegroup.mall.util.CommonResult;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/consumer-register")
public class ConsumerRegisterHandler extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        resp.setCharacterEncoding("UTF-8");
        String account = req.getParameter("username");
        String password = req.getParameter("password");

        System.out.println("test" + account + " " + password);

        ConsumerDao consumerDao = new ConsumerDaoImpl();
        int affectRow = consumerDao.register(account, password);
        if (affectRow > 0) resp.getWriter()
                .write(new Gson()
                        .toJson(CommonResult.success()));
        else resp.getWriter()
                .write(new Gson()
                        .toJson(CommonResult.fail(400,"用户名已存在请重试")));
    }
}
